﻿using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight.CommandWpf;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
namespace AgileLogistics.ViewModel
{
    [PropertyChanged.AddINotifyPropertyChangedInterface]
  public class MenuViewModel: ViewModelBase
    {

        Model.DBContext context = new Model.DBContext();
         

        Model.Repository<Model.Menus> rep = new Model.Repository<Model.Menus>();
        public MenuViewModel()
        {
            MenusList = new ObservableCollection<Model.Menus>( rep.GetList());

            MenuClassList = new ObservableCollection<Model.MenuClass>(context.DB.Queryable<Model.MenuClass>().ToList());


            
        }



        #region 菜单切换
        private RelayCommand<string> menusChangeCommand => new Lazy<RelayCommand<string>>(() => new RelayCommand<string>(MenuChangeFunc)).Value;

        //private RelayCommand<string> menuChange => new Lazy<RelayCommand<string>>(() => new RelayCommand<string>(MenuChangeFunc)).Value;
        public RelayCommand<string> MenusChangeCommand
        {
            get
            {
                return menusChangeCommand;
            }

        }


        /// <summary>
        /// 菜单切换
        /// </summary>
        /// <param name="MenuHeader"></param>
        private void MenuChangeFunc(string MenuHeader)
        {
            Page page = new Page();
            switch (MenuHeader)
            {
                default:
                    break;
                case "发起投票":
                    page = new View.Meishi.CreatePollPage();
                    break;
                case "历史投票":
                    page = new View.Meishi.HistoryPollPage();
                    break;
                case "当前投票":
                    page = new View.Meishi.PallPage();
                    break;

                case "创建菜品":
                    page = new View.Meishi.CURD.CreatePage();
                    break;
                case "菜品列表":
                    page = new View.Meishi.CURD.ListPage();
                    break;

            }

            GalaSoft.MvvmLight.Messaging.Messenger.Default.Send<Page>(page, "MenuChangePage");
        }


        #endregion




        public ObservableCollection<Model.Menus> MenusList { get; set; }
        public ObservableCollection<Model.MenuClass> MenuClassList { get; set; }
        public  Model.Menus CurrentMenu { get; set; }

        public Model.MenuClass CurrentMenuClass { get; set; }


        #region 菜品编号生成规则
        public int GenerateMenuId(int classId)
        {
            int maxId = context.DB.Queryable<Model.Menus>().Where(m=>m.ClassId ==classId).Max(m=>m.Id) +1;
            string strMaxId = maxId.ToString();

            if(strMaxId.Length<4)
            {
                strMaxId ="001";
            }
            strMaxId = strMaxId.Substring(strMaxId.Length-3,3);

            string strNewId =  classId.ToString() + strMaxId.ToString() ;
            return int.Parse(strNewId);
        }
        #endregion




        #region Home数据
        public int SumMenuCount { get; set; }
        public int SumPollCount { get; set; }
        public int SumPollsCount { get; set; }




        public List<int> NewMenuList { get; set; }
        public List<int> PollCountList { get; set; }
        public List<int> XiaolvList { get; set; }




        public List<string> WeekLabels = new List<string>();
        #endregion

    }
}
